import Logger from "../base/logger";
import {
    getBrowserClientInfo
} from "./tool";


function Handler() {
    this.next = null;
    this.setNext = function (_handler) {
        this.next = _handler;
    };
    // eslint-disable-next-line no-unused-vars
    this.answerRequest = function (videoLabel, streamOptions, isRecordMix, isRecordSelf, success, failed) {

    }
}



var amsHandler = function () {};
amsHandler.prototype = new Handler();
amsHandler.prototype.answerRequest = function (amsClient, success, failed) {
    amsClient.answerCall(function (status, resp) {
        if (status === 'ok') {
            Logger.info('answer ams call success', resp);
            success();
        } else {
            Logger.warning('answer call failed', resp);
            failed(resp);
        }
    })
};


var ccsHandler = function () {};
ccsHandler.prototype = new Handler();
ccsHandler.prototype.answerRequest = function (ccsClient, url, token, sessionId, success, failed) {
    ccsClient.connect(url, token, (resp) => {
        Logger.info(`connect to ccs success,${new Date()},${resp}`);
        let joinData = {
            media: 'video',
            userData: {
                skill: '',
                specifyAgent: ''
            },
            sessionId: sessionId,
            clientInfo: getBrowserClientInfo()
        };
        ccsClient.joinSession(joinData, (joinResult) => {
            Logger.info(`join session ${sessionId} success,${JSON.stringify(joinResult)},${new Date()}`);
            success(joinResult);
        }, (error) => {
            Logger.info(`join session ${sessionId} error,${error},${new Date()}`);
            failed(error);
        });
    }, (error) => {
        Logger.info(`connect to ccs error,${error},${new Date()}`);
        failed(error);
    });
};


export {
    amsHandler,
    ccsHandler
}